<template>
	<gui-page ref="guipage" :apiLoadingStatus="apiLoadingStatus" :refresh="true" @reload="reload" :loadmore="true"
		@loadmorefun="getData">
		<view slot="gBody">
			<gui-back></gui-back>
			<view class="data-module">
				<view class="title-view">
					<!-- 居中标题 -->
					<view class="gui-flex gui-rows gui-nowrap gui-justify-content-center gui-align-items-center">
						<text class="gui-color-gray gui-icons small-text">&#xe69b;</text>
						<text class="gui-primary-color gui-h6 title1-margin gui-title-text">{{curriculumName}}</text>
						<text class="gui-color-gray gui-icons small-text">&#xe69b;</text>
					</view>
					<view class="gui-flex gui-rows gui-nowrap gui-align-items-center">
						<view class="gui-title-line"></view>
						<text class="gui-primary-color gui-h6"
							style="padding-left:50rpx; padding-right:50rpx;">{{chapterName}}</text>
						<view class="gui-title-line"></view>
					</view>
				</view>
				<gui-empty v-if="noData">
					<view slot="img" class="gui-flex gui-rows gui-justify-content-center">
						<!-- 请根据您的项目要求制作并更换为空图片 -->
						<image class="gui-empty-img" src="/static/noImg.png" mode="widthFix"></image>
					</view>
					<text slot="text" class="gui-text gui-block-text gui-text-center gui-margin-top"
						style="color:#666;padding: 0 100rpx;">暂无数据！</text>
				</gui-empty>
				<view class="data-list">

					<view class="data-item bgfff" v-for="(item,index) in dataList" :key="item.id">
						<cpdd-swipe-action>
							<cpdd-swipe-action-item :index="index" @click="handleClick($event, index)">
								<view class="problem-list">
									<view class="problem-item">
										<view class="problem-title flex">
											<text>{{index+1}}.</text>
											<text>{{item.problem}}？</text>
											<view class="status cfff"
												:style="{'background':getType(item.topicType,'color')}">
												{{getType(item.topicType,'text')}}</view>
										</view>
										<view class="option-list flex-ac-wrap" v-if="item.params&&item.params.options">
											<view class="option-item flex-ac"
												v-for="(opItem,opIdx) in item.params.options">
												<text style="position: relative;top: 4rpx;">{{opItem.option}}.</text>
												<text class="flex1">{{opItem.content}}</text>
											</view>
										</view>
										<view class="answer">
											<text>正确答案：</text>
											<text>{{item.answer}}</text>
										</view>
									</view>
								</view>
							</cpdd-swipe-action-item>
						</cpdd-swipe-action>
					</view>
				</view>
			</view>
			<view class="com-addBtn flex-ac-jc" @click="goOptions('add')">
				<text class="gui-icons f40 cfff">&#xe6c7;</text>
			</view>
		</view>
	</gui-page>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		columns
	} from './data.js'
	export default {
		data() {
			return {
				query: { //公用查询
					curriculumId: '',
					chapterId: '',
					pageNum: 1,
					pageSize: 10
				},
				curriculumName: '',
				chapterName: '',
				totalPage: 1, //总页数
				apiLoadingStatus: false, //加载
				dataList: [], //数据
				noData: false, //是否无数据
			}
		},
		computed: {
			columns() {
				return columns
			}
		},
		onLoad(options) {
			this.query.chapterId = options.id ? options.id : ''
			this.chapterName = options.name ? options.name : ''
			this.query.curriculumId = options.curriculumId ? options.curriculumId : ''
			this.curriculumName = options.curriculumName ? options.curriculumName : ''
			this.getData()
			uni.$on('reFlashList', () => {
				this.reload(false)
			})
		},
		onUnload() {
			uni.$off('reFlashList')
		},
		methods: {
			getType(status, type) {
				//退款的流程 1.申请退款 2.已同意退款 3.退货中 4.已退货 5.已完成 6.拒绝
				let textStatus = ''
				let color = ''
				switch (status) {
					case 'Single':
						textStatus = '单选'
						color = '#32CD32'
						break;
					case 'Multi':
						textStatus = '多选'
						color = '#2979ff'
						break;
					case 'Judge':
						textStatus = '判断'
						color = '#FF0000'
						break;
					default:
						break;
				}
				return type == 'text' ? textStatus : color
			},
			handleClick(e, index) { //左滑操作列表点击
				const item = this.dataList[index]
				if (!item || !item.id) return this.$u.msg('未获取到id')
				if (e.content.text == '查看') {
					this.goOptions('look', item.id, item)
				} else if (e.content.text == '编辑') {
					this.goOptions('edit', item.id, item)
				} else if (e.content.text == '删除') {
					this.handleDel(item.id)
				}
			},
			goOptions(op, id, item) { //操作跳转详情
				let idParams = op != 'add' ? `&id=${id}` : '' //判断要不要带id过去
				this.$u.to(`./form?op=${op}` + idParams + '&curriculumId=' + this.query.curriculumId + '&chapterId=' + this
					.query.chapterId + '&item=' + JSON.stringify(item))
			},
			handleDel(id) { //删除
				this.$http.delete(`/training/question/${id}`).then(res => {
					let result = res.data
					console.log('', result)
					this.$u.msg('删除成功！')
					this.reload(false);
				})
			},
			getData: function(isReload) { //获取列表数据
				this.apiLoadingStatus = true;
				console.log('加载函数运行，页码 : ' + this.query.pageNum);
				this.$http.get('/training/question/list', {
					...this.query,
					...this.searchForm
				}).then(res => {
					let result = res.rows
					console.log('列表', result)
					this.totalPage = Math.ceil(res.total / this.query.pageSize);
					if (this.query.pageNum >= 2) {
						this.dataList = this.dataList.concat(result);
						// 加载完成后停止加载动画
						this.$refs.guipage.stoploadmore();
						// 假定加载了全部数据，通知组件不再加载更多
						if (this.query.pageNum >= this.totalPage) {
							this.$refs.guipage.nomore();
						}
					}
					// 第一页 有可能是第一次加载或者刷新
					else {
						this.dataList = [];
						this.dataList = result;
						this.noData = result.length ? false : true;
						if (this.query.pageNum >= this.totalPage) {
							this.$refs.guipage.nomore();
						}
						// 刷新
						if (isReload) {
							this.$refs.guipage.endReload();
						}
					}
					this.query.pageNum++;
					this.apiLoadingStatus = false;
				})
			},
			reload: function(isReload = true) {
				this.query.pageNum = 1;
				this.getData(isReload);
			},
		}
	}
</script>

<style lang="less">
	.problem-item {
		padding: 25rpx 0;

		.problem-title {
			.status {
				padding: 4rpx 14rpx;
				border-radius: 20rpx 0 20rpx 0;
				font-size: 24rpx;
				margin-left: 10rpx;
			}
		}

		.option-list {
			padding: 20rpx 0 0 30rpx;

			.option-item {
				width: 50%;
				margin-bottom: 20rpx;
			}
		}

		.answer {
			padding: 0 30rpx;
		}
	}
</style>